package com.itboye.ihomebank.activity.keytwo;

import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.volley.ServerError;
import com.android.volley.TimeoutError;
import com.itboye.ihomebank.R;
import com.itboye.ihomebank.adapter.KaiSuoJiLuAdapter;
import com.itboye.ihomebank.adapter.KaiSuoJiLuTwoAdapter;
import com.itboye.ihomebank.base.BaseOtherActivity;
import com.itboye.ihomebank.base.MyApplcation;
import com.itboye.ihomebank.bean.KaiSuoJiLu;
import com.itboye.ihomebank.bean.KaiSuoJiLuTwo;
import com.itboye.ihomebank.bean.ResultEntity;
import com.itboye.ihomebank.bean.SIDBean;
import com.itboye.ihomebank.constants.SPContants;
import com.itboye.ihomebank.custom.XListView;
import com.itboye.ihomebank.presenter.KeyPresenter;
import com.itboye.ihomebank.presenter.KeyTwoPresenter;
import com.itboye.ihomebank.presenter.UserPresenter;
import com.itboye.ihomebank.util.ByAlert;
import com.itboye.ihomebank.util.SPUtils;
import com.itboye.ihomebank.util.TimesUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

/**
 * 开锁记录-白马锁
 */
public class KaiSuoJiLuActivity extends BaseOtherActivity   implements Observer,XListView.IXListViewListener {
    View v_statusbar;
    ImageView key_title_finish;
    TextView key_title_title;

    KaiSuoJiLuTwoAdapter adapter;

    XListView jilu_listview;

    private List<KaiSuoJiLuTwo.KaiSuoJiLuTwoList> beans=new ArrayList<>();
    KeyTwoPresenter keyPresenter;

    private String uid;

    private String lockId;

    private int page;//页码
    private String pageSize = "10";//每页显示的条数

    TextView nodata;

    ImageView key_title_shezhi;
    @Override
    protected int layout_id() {
        return R.layout.activity_kaisuojilu;
    }

    @Override
    protected void onInitController() {
        jilu_listview.setXListViewListener(this);
        jilu_listview.setPullLoadEnable(true);
        jilu_listview.setPullRefreshEnable(true);

        adapter = new KaiSuoJiLuTwoAdapter(this, beans, R.layout.item_kaisuo_jilu);
        jilu_listview.setAdapter(adapter);
    }

    @Override
    protected void onInit() {
        setStatusBarHeight(v_statusbar);
        key_title_shezhi.setVisibility(View.GONE);
        key_title_title.setText("开锁记录");
        lockId = getIntent().getStringExtra("lockid");
        uid = SPUtils.get(this, null, SPContants.USER_ID, "") + "";
        keyPresenter=new KeyTwoPresenter(this);
        page = 1;
        String sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
        if ("".equals(sid)){
            keyPresenter.onKaiSuoJiLu(uid, lockId, page + "", pageSize);

        }else {
            keyPresenter.onKaiSuoJiLu(sid, lockId, page + "", pageSize);

        }
    }

    @Override
    protected void onReadIntent(Bundle bundle) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.key_title_finish:
                finish();
                break;
            default:
                break;
        }

    }
    @Override
    public void update(Observable observable, Object data) {
        ResultEntity entity = handlerError(data);
        if (entity != null) {
            if (entity.getEventType() == KeyTwoPresenter.kaisuo_success) {
                KaiSuoJiLuTwo bean=(KaiSuoJiLuTwo) entity.getData();
                if (page == 1) {// 当页码数为1时，清空，以免下拉时重复加载数据
                    beans.clear();
                }
                beans.addAll(bean.getData());
                if (beans.size() <= 0) {
                    nodata.setVisibility(View.VISIBLE);
                    jilu_listview.setVisibility(View.GONE);
                } else {
                    nodata.setVisibility(View.GONE);
                    jilu_listview.setVisibility(View.VISIBLE);
                    adapter.notifyDataSetChanged();
                }
                try {
                    stopLoad();
                } catch (Exception e) {
                }
            }else if(entity.getEventType() == KeyTwoPresenter.kaisuo_fail){
                try {
                    stopLoad();
                } catch (Exception e) {
                }
                if (entity.getData() instanceof ServerError ||entity.getData() instanceof TimeoutError){//服务器失败
                    //切换 锁备用服务器
                    showAlert();
                }
            }else if (entity.getEventType()== UserPresenter.By_GETSID_success){//获取sid 成功
                  SIDBean data1 = (SIDBean) entity.getData();
                  SPUtils.put(MyApplcation.ctx,null, SPContants.LOCK_SESSION,data1.getSid());
                  SPUtils.put(MyApplcation.ctx,null,SPContants.ISLOCK,true);
                  toRefreshData(data1.getSid());
              }else if (entity.getEventType()==UserPresenter.By_GETSID_fail){
                  ByAlert.alert(entity.getMsg());
              }
        }
    }

    private void toRefreshData(String sid) {
        if ("".equals(sid)){
            keyPresenter.onKaiSuoJiLu(uid, lockId, page + "", pageSize);

        }else {
            keyPresenter.onKaiSuoJiLu(sid, lockId, page + "", pageSize);

        }

    }

    @Override
    public void onRefresh() {
        page = 1;
        keyPresenter = new KeyTwoPresenter(this);
        String sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
        if ("".equals(sid)){
            keyPresenter.onKaiSuoJiLu(uid, lockId, page + "", pageSize);

        }else {
            keyPresenter.onKaiSuoJiLu(sid, lockId, page + "", pageSize);

        }

    }

    @Override
    public void onLoadMore() {
        page++;
        keyPresenter = new KeyTwoPresenter(this);
        String sid= SPUtils.get(MyApplcation.ctx,null,SPContants.LOCK_SESSION,"")+"";
        if ("".equals(sid)){
            keyPresenter.onKaiSuoJiLu(uid, lockId, page + "", pageSize);

        }else {
            keyPresenter.onKaiSuoJiLu(sid, lockId, page + "", pageSize);

        }

    }
    /**
     * 停止加载
     */
    public void stopLoad() {
        jilu_listview.stopLoadMore();
        jilu_listview.stopRefresh();
    }
}
